//
//  BPMessageViewController.swift
//  BPWeiBo
//
//  Created by apple on 16/4/6.
//  Copyright © 2016年 apple. All rights reserved.

import UIKit
//继承基类BPBaseController
class BPMessageViewController: BPBaseController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.init(red: 236 / 255.0, green: 236 / 255.0, blue: 236 / 255.0, alpha: 1)
        setUpUI()
        
        if userLogin {
        setBarItem()
        setBadgeValue()
        }
        

    }
    
    //MARK:设置访客界面的文字和右边的按钮
    private func setUpUI() {
        visiterView.setBaseInfo("花自飘零水自流,一种相思,两处闲愁;此情无计可消除,才下眉头,却上心头", imgName: "visitordiscover_image_message", cycleicon: UIImage())
        
    }
    
    //MARK:设置导航条的按钮
    private func setBarItem() {
            //设置右边的导航条按钮
            let rightBarItem = UIBarButtonItem(imgName:"navigationbar_friendsearch", title: "发现群", target: self, action: "discoverCrowd")
            
            navigationItem.rightBarButtonItem = rightBarItem
        
    }
    
    
    //MARK:实现点击发现群
    @objc private func discoverCrowd() {
        print("faxian")
    }

    // MARK: - Table view data source
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 0
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    
    //设置tabbarItem上名显示badgeValue
    private func setBadgeValue() {
        self.tabBarItem.badgeValue = "10"
        let item = self.tabBarItem
        let tabBar = self.tabBarController?.tabBar as! BPTabBar
        let img = UIImage(named: "main_badge")
        
        
        var index = 1
        
        for subView in tabBar.subviews {
            
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                //定位到点击的视图
                if index == item.tag - baseTag {
                    for subView1 in subView.subviews {
                        
                        if subView1.isKindOfClass(NSClassFromString("_UIBadgeView")!) {
                            
                            for subView2 in subView1.subviews {
                                
                                if subView2.isKindOfClass(NSClassFromString("_UIBadgeBackground")!) {
                                
                                    //print("ok")
                                    //控制台测试可以知道_UIBadgeBackground有一个_image的属性.利用这个属性给设置我们自定义的图片就可以了
  
                                   subView2.setValue(img, forKey: "_image")
                                }
                            
                            }
                            
                            
                        }
                        
                    }
                }
                index += 1
            }
        }
        
        
    }
}


